from docx import Document

import os

current_dir = os.getcwd()
filenames = []

files = os.listdir(current_dir)

for name in files:
    if name.endswith('docx'):
        filenames.append(current_dir+'\\'+name)

newPath = current_dir+'\\'+'new'
if not os.path.exists(newPath):
    os.makedirs(newPath)

for item in filenames:
    try:
        document = Document(item)
        for section in document.sections:
            header = section.header
            header.is_linked_to_previous = True

            footer = section.footer
            footer.is_linked_to_previous = True
        document.save(newPath + '\\' + item.split('\\')[-1])
        print(item.split('\\')[-1] + '-----打包成功！')
    except Exception as e:
        print(item.split('\\')[-1] + '-----打包失败，原因如下！')
        print(e)
input('按任意键退出')